home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / gfx / show / vmpeg.lha / src / mpeg2dec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-06  |  20.7 KB  |  838 lines

  1. /* mpeg2dec.c, main(), initialization, option processing                    */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33.  
  34. #include <exec/types.h>
  35. #include <graphics/modeid.h>
  36. #undef GLOBAL
  37.  
  38. #define GLOBAL
  39. #include "config.h"
  40. #include "global.h"
  41.  
  42.  
  43. /* private prototypes */
  44. static int  video_sequence _ANSI_ARGS_((int *framenum));
  45. static int Decode_Bitstream _ANSI_ARGS_((void));
  46. static int  Headers _ANSI_ARGS_((void));
  47. static void Initialize_Sequence _ANSI_ARGS_((void));
  48. static void Initialize_Decoder _ANSI_ARGS_((void));
  49. static void Deinitialize_Sequence _ANSI_ARGS_((void));
  50. static void Process_Options _ANSI_ARGS_((int argc, char *argv[]));
  51.  
  52. extern int Decode_Audio();
  53.  
  54.  
  55. #if OLD
  56. static int  Get_Val _ANSI_ARGS_((char *argv[]));
  57. #endif
  58.  
  59. /* #define DEBUG */
  60.  
  61. static void Clear_Options();
  62. #ifdef DEBUG
  63. static void Print_Options();
  64. #endif
  65.  
  66. FILE *pipe_file;
  67.  
  68.  
  69.  
  70. int main(int argc,char *argv[])
  71. {
  72.   int ret, code;
  73.  
  74.   Clear_Options();
  75.  
  76.   /* decode command line arguments */
  77.   Process_Options(argc,argv);
  78.  
  79. #ifdef DEBUG
  80.   Print_Options();
  81. #endif
  82.  
  83.   ld = &base; /* select base layer context */
  84.  
  85.   /* open MPEG base layer bitstream file(s) */
  86.   /* NOTE: this is either a base layer stream or a spatial enhancement stream */
  87.   if ((base.Infile=fopen(Main_Bitstream_Filename,"r"))==NULL)
  88.   {
  89.     fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
  90.     exit(1);
  91.   }
  92.  
  93.  
  94.   if(base.Infile != 0)
  95.   {
  96.     Initialize_Buffer(); 
  97.   
  98.     if(Show_Bits(8)==0x47)
  99.     {
  100.       sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
  101.       Error(Error_Text);
  102.     }
  103.  
  104.     next_start_code();
  105.     code = Show_Bits(32);
  106.  
  107.     switch(code)
  108.     {
  109.     case SEQUENCE_HEADER_CODE:
  110.       break;
  111.     case PACK_START_CODE:
  112.       System_Stream_Flag = 1;
  113.     case VIDEO_ELEMENTARY_STREAM:
  114.       System_Stream_Flag = 1;
  115.       break;
  116.     default:
  117.       sprintf(Error_Text,"Unable to recognize stream type %d\n",code);
  118.       Error(Error_Text);
  119.       break;
  120.     }
  121.  
  122.     fseek(base.Infile, 0, SEEK_SET);
  123.     Initialize_Buffer(); 
  124.   }
  125.  
  126.   if(base.Infile!=0)
  127.   {
  128.     fseek(base.Infile, 0, SEEK_SET);
  129.   }
  130.  
  131.   Initialize_Buffer(); 
  132.  
  133.   if(Two_Streams)
  134.   {
  135.     ld = &enhan; /* select enhancement layer context */
  136.  
  137.     if ((enhan.Infile = fopen(Enhancement_Layer_Bitstream_Filename,"r"))==NULL)
  138.     {
  139.       sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
  140.         Enhancement_Layer_Bitstream_Filename);
  141.  
  142.       Error(Error_Text);
  143.     }
  144.  
  145.     Initialize_Buffer();
  146.     ld = &base;
  147.   }
  148.  
  149.   Initialize_Decoder();
  150.  
  151.   if (Enable_Sound) {
  152.     if (!audio_init())
  153.       Enable_Sound = 0;
  154.   }
  155.   ret = Decode_Bitstream();
  156.   if (Enable_Sound)
  157.     audio_cleanup();
  158.  
  159.   fclose(base.Infile);
  160.  
  161.   if (Two_Streams)
  162.     fclose(enhan.Infile);
  163.  
  164.   return 0;
  165. }
  166.  
  167.  
  168. static void Initialize_Decoder()
  169. /* IMPLEMENTATION specific rouintes */
  170. {
  171.   int i;
  172.  
  173.   /* Clip table */
  174.   if (!(Clip=(unsigned char *)malloc(1024)))
  175.     Error("Clip[] malloc failed\n");
  176.  
  177.   Clip += 384;
  178.  
  179.   for (i=-384; i<640; i++)
  180.     Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
  181.  
  182.   /* IDCT */
  183.   if (Reference_IDCT_Flag)
  184.     Initialize_Reference_IDCT();
  185.   else
  186.     Initialize_Fast_IDCT();
  187.  
  188. }
  189.  
  190.  
  191. static void Initialize_Sequence()
  192. /* mostly IMPLEMENTATION specific rouintes */
  193. {
  194.   int cc, size;
  195.   static int Table_6_20[3] = {6,8,12};
  196.  
  197.   /* check scalability mode of enhancement layer */
  198.   if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP))
  199.     Error("unsupported scalability mode\n");
  200.  
  201.   /* force MPEG-1 parameters for proper decoder behavior */
  202.   /* see ISO/IEC 13818-2 section D.9.14 */
  203.   if (!base.MPEG2_Flag)
  204.   {
  205.     progressive_sequence = 1;
  206.     progressive_frame = 1;
  207.     picture_structure = FRAME_PICTURE;
  208.     frame_pred_frame_dct = 1;
  209.     chroma_format = CHROMA420;
  210.     matrix_coefficients = 5;
  211.   }
  212.  
  213.   /* round to nearest multiple of coded macroblocks */
  214.   /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */
  215.   mb_width = (horizontal_size+15)/16;
  216.   mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32)
  217.                                         : (vertical_size+15)/16;
  218.   if (mb_width==0 && mb_height==0) {
  219.     mb_width = 22;
  220.     mb_height = 18;
  221.   }
  222.  
  223.   Coded_Picture_Width = 16*mb_width;
  224.   Coded_Picture_Height = 16*mb_height;
  225.  
  226.   /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */
  227.   Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width
  228.                                            : Coded_Picture_Width>>1;
  229.   Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height
  230.                                             : Coded_Picture_Height>>1;
  231.   
  232.   /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */
  233.   block_count = Table_6_20[chroma_format-1];
  234.  
  235.   for (cc=0; cc<3; cc++)
  236.   {
  237.     if (cc==0)
  238.       size = Coded_Picture_Width*Coded_Picture_Height;
  239.     else
  240.       size = Chroma_Width*Chroma_Height;
  241.  
  242.     if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
  243.       Error("backward_reference_frame[] malloc failed\n");
  244.  
  245.     if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
  246.       Error("forward_reference_frame[] malloc failed\n");
  247.  
  248.     if (!(auxframe[cc] = (unsigned char *)malloc(size)))
  249.       Error("auxframe[] malloc failed\n");
  250.  
  251.     if(Ersatz_Flag)
  252.       if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
  253.         Error("substitute_frame[] malloc failed\n");
  254.  
  255.  
  256.     if (base.scalable_mode==SC_SPAT)
  257.     {
  258.       /* this assumes lower layer is 4:2:0 */
  259.       if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
  260.         Error("llframe0 malloc failed\n");
  261.       if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
  262.         Error("llframe1 malloc failed\n");
  263.     }
  264.   }
  265.  
  266.   /* SCALABILITY: Spatial */
  267.   if (base.scalable_mode==SC_SPAT)
  268.   {
  269.     if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short))))
  270.       Error("lltmp malloc failed\n");
  271.   }
  272.  
  273. #ifdef DISPLAY
  274.   if (Output_Type>=T_AMIGADIRECT)
  275.   {
  276.     Initialize_Display_Process("");
  277.     Initialize_Dither_Matrix();
  278.   }
  279. #endif /* DISPLAY */
  280.  
  281.   speed = 0;
  282. }
  283.  
  284.  
  285. void Error(char *text)
  286. {
  287.   fprintf(stderr,text);
  288.   exit(1);
  289. }
  290.  
  291.  
  292. void Print_Bits(int code,int bits,int len)
  293. /* Trace_Flag output */
  294. {
  295.   int i;
  296.  
  297.   for (i=0; i<len; i++)
  298.     printf("%d",(code>>(bits-1-i))&1);
  299. }
  300.  
  301.  
  302.  
  303. static void Process_Options(int argc,char *argv[])
  304. /* option processing */
  305. {
  306.   int i, LastArg, NextArg;
  307.  
  308.   /* at least one argument should be present */
  309.   if (argc<2)
  310.   {
  311.     printf("vmpeg V0.3\n----------\nMPEG Video Decoder %s\n",Author);
  312.     printf("MPEG Audio Decoder (C) 1991,92 MPEG/Audio Software Simulation Group\n"
  313.            "Amiga WarpOS version and AHI support in 1999 by Frank Wille\n"
  314.            "based on the work of Jesper Svennevid\n");
  315.     printf("\nUsage:  vmpeg {options}\n\
  316. Options: -a         play all frames at maximum speed (disables sound)\n\
  317.          -b  file   main bitstream (base or spatial enhancement layer)\n\
  318.          -cn file   conformance report (n: level)\n\
  319.          -dn        define frames per second for replay\n\
  320.          -e  file   enhancement layer bitstream (SNR or Data Partitioning)\n\
  321.          -f         store/display interlaced video in frame format\n\
  322.          -g         concatenated file format for substitution method (-x)\n\
  323.          -h         double pixel-size\n\
  324.          -in file   information & statistics report  (n: level)\n\
  325.          -j         show fps counter\n\
  326.          -l  file   file name pattern for lower layer sequence\n\
  327.          -mn        modeid for screenmode (format: -m0x0000)\n\
  328.          -on file   output format (0:YUV 1:SIF 2:TGA 3:PPM 4:ScreenDirect\n\
  329.                                    5:ScreenCompatible 6:Picasso96 PIP)\n\
  330.          -q         disable warnings to stderr\n\
  331.          -r         use double precision reference IDCT\n\
  332.          -s         enable decoding of layer 1/2 sound \n\
  333.          -t         enable low level tracing to stdout\n\
  334.          -u  file   print user_data to stdio or file\n\
  335.          -vn        verbose output (n: level)\n\
  336.          -x  file   filename pattern of picture substitution sequence\n\n\
  337. File patterns:  for sequential filenames, \"printf\" style, e.g. rec%%d\n\
  338.                  or rec%%d%%c for fieldwise storage\n\
  339. Levels:        0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n\
  340. Example:       vmpeg -o4 -s -b bitstream.mpg <- decode a bitstream to\n\
  341.                                                 screen with sound enabled.\
  342.          \n");
  343.     exit(0);
  344.   }
  345.  
  346.  
  347.   Output_Type = -1;
  348.   i = 1;
  349.  
  350.   /* command-line options are proceeded by '-' */
  351.  
  352.   while(i < argc)
  353.   {
  354.     /* check if this is the last argument */
  355.     LastArg = ((argc-i)==1);
  356.  
  357.     /* parse ahead to see if another flag immediately follows current
  358.        argument (this is used to tell if a filename is missing) */
  359.     if(!LastArg)
  360.       NextArg = (argv[i+1][0]=='-');
  361.     else
  362.       NextArg = 0;
  363.  
  364.     /* second character, [1], after '-' is the switch */
  365.     if(argv[i][0]=='-')
  366.     {
  367.       switch(toupper(argv[i][1]))
  368.       {
  369.         /* third character. [2], is the value */
  370.       case 'A':
  371.         AllFrames = 1;
  372.         break;
  373.  
  374.       case 'B':
  375.         Main_Bitstream_Flag = 1;
  376.  
  377.         if(NextArg || LastArg)
  378.           printf("ERROR: -b must be followed the main bitstream filename\n");
  379.         else
  380.           Main_Bitstream_Filename = argv[++i]; 
  381.  
  382.         break;
  383.  
  384.  
  385.       case 'C':
  386.  
  387. #ifdef VERIFY
  388.         Verify_Flag = atoi(&argv[i][2]); 
  389.  
  390.         if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
  391.         {
  392.           printf("ERROR: -c level (%d) out of range [%d,%d]\n",
  393.             Verify_Flag, NO_LAYER, ALL_LAYERS);
  394.           exit(ERROR);
  395.         }
  396. #else  /* VERIFY */
  397.         printf("This program not compiled for Verify_Flag option\n");
  398. #endif /* VERIFY */
  399.         break;
  400.  
  401.       case 'D':
  402.         if (!sscanf(&argv[i][2],"%lf",&User_Defined_FPS))
  403.           User_Defined_FPS = 0.0;
  404.         break;
  405.  
  406.       case 'E':
  407.         Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
  408.                      
  409.         if(NextArg || LastArg)
  410.         {
  411.           printf("ERROR: -e must be followed by filename\n");
  412.           exit(ERROR);
  413.         }
  414.         else
  415.           Enhancement_Layer_Bitstream_Filename = argv[++i]; 
  416.  
  417.         break;
  418.  
  419.       case 'F':
  420.         Frame_Store_Flag = 1;
  421.         break;
  422.  
  423.       case 'G':
  424.         Big_Picture_Flag = 1;
  425.         break;
  426.  
  427.       case 'H':
  428.         Double_Pixels = 2L;
  429.         break;
  430.  
  431.       case 'I':
  432. #ifdef VERIFY
  433.         Stats_Flag = atoi(&argv[i][2]); 
  434. #else /* VERIFY */
  435.         printf("WARNING: This program not compiled for -i option\n");
  436. #endif /* VERIFY */     
  437.         break;
  438.  
  439.       case 'J':
  440.         Fps_Flag = 1;
  441.         break;
  442.  
  443.       case 'L':  /* spatial scalability flag */
  444.         Spatial_Flag = 1;
  445.  
  446.        if(NextArg || LastArg)
  447.        {
  448.          printf("ERROR: -l must be followed by filename\n");
  449.          exit(ERROR);
  450.        }
  451.        else
  452.          Lower_Layer_Picture_Filename = argv[++i]; 
  453.  
  454.         break;
  455.  
  456.       case 'M':
  457.  
  458.         if(!sscanf(&argv[i][2],"%lx",&Mode_ID))
  459.           Mode_ID = INVALID_ID;
  460.  
  461.         break;
  462.  
  463.       case 'O':
  464.   
  465.         Output_Type = atoi(&argv[i][2]); 
  466.   
  467.         if (Output_Type>=T_AMIGADIRECT) {
  468.           Output_Picture_Filename = "";  /* no need of filename */
  469.         }
  470.         else if (NextArg || LastArg) {
  471.           printf("ERROR: -o must be followed by filename\n");
  472.           exit(ERROR);
  473.         }
  474.         else
  475.         /* filename is separated by space, so it becomes the next argument */
  476.           Output_Picture_Filename = argv[++i];
  477.  
  478.         break;
  479.  
  480.       case 'Q':
  481.         Quiet_Flag = 1;
  482.         break;
  483.  
  484.       case 'R':
  485.         Reference_IDCT_Flag = 1;
  486.         break;
  487.  
  488.       case 'S':
  489.         Enable_Sound = 1;
  490.         break;
  491.  
  492.       case 'T':
  493. #ifdef TRACE
  494.         Trace_Flag = 1;
  495. #else /* TRACE */
  496.         printf("WARNING: This program not compiled for -t option\n");
  497. #endif /* TRACE */
  498.         break;
  499.  
  500.       case 'U':
  501.         User_Data_Flag = 1;
  502.  
  503.       case 'V':
  504. #ifdef VERBOSE
  505.         Verbose_Flag = atoi(&argv[i][2]); 
  506. #else /* VERBOSE */
  507.         printf("This program not compiled for -v option\n");
  508. #endif /* VERBOSE */
  509.         break;
  510.  
  511.  
  512.       case 'X':
  513.         Ersatz_Flag = 1;
  514.  
  515.        if(NextArg || LastArg)
  516.        {
  517.          printf("ERROR: -x must be followed by filename\n"); 
  518.          exit(ERROR);
  519.        }
  520.        else
  521.         Substitute_Picture_Filename = argv[++i]; 
  522.  
  523.         break;
  524.  
  525.  
  526.  
  527.       default:
  528.         fprintf(stderr,"undefined option -%c ignored. Exiting program\n", 
  529.           argv[i][1]);
  530.  
  531.         exit(ERROR);
  532.     
  533.       } /* switch() */
  534.     } /* if argv[i][0] == '-' */
  535.     
  536.     i++;
  537.  
  538.     /* check for bitstream filename argument (there must always be one, at the very end
  539.      of the command line arguments */
  540.  
  541.   } /* while() */
  542.  
  543.  
  544.   /* options sense checking */
  545.   if (Main_Bitstream_Flag!=1)
  546.     printf("There must be a main bitstream specified (-b filename)\n");
  547.  
  548.   if (AllFrames || User_Defined_FPS>0.0)
  549.     Enable_Sound = 0;
  550.  
  551.   /* force display process to show frame pictures */
  552.   if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
  553.     Display_Progressive_Flag = 1;
  554.   else
  555.     Display_Progressive_Flag = 0;
  556.  
  557. #ifdef VERIFY
  558.   /* parse the bitstream, do not actually decode it completely */
  559.   
  560.  
  561. #if 0
  562.   if(Output_Type==-1)
  563.   {
  564.     Decode_Layer = Verify_Flag;
  565.     printf("FYI: Decoding bitstream elements up to: %s\n", 
  566.       Layer_Table[Decode_Layer]);
  567.   }
  568.   else
  569. #endif
  570.     Decode_Layer = ALL_LAYERS;
  571.  
  572. #endif /* VERIFY */
  573.  
  574.   /* no output type specified */
  575.   if(Output_Type==-1)
  576.   {
  577.     Output_Type = 9; 
  578.     Output_Picture_Filename = "";
  579.   }
  580.  
  581.  
  582. #ifdef DISPLAY
  583.   if (Output_Type>=T_AMIGADIRECT)
  584.   {
  585.     if(Frame_Store_Flag)
  586.       Display_Progressive_Flag = 1;
  587.     else
  588.       Display_Progressive_Flag = 0;
  589.  
  590.     Frame_Store_Flag = 1; /* to avoid calling dither() twice */
  591.   }
  592. #endif
  593.  
  594.  
  595. }
  596.  
  597.  
  598. static int Headers()
  599. {
  600.   int ret;
  601.  
  602.   ld = &base;
  603.   
  604.  
  605.   /* return when end of sequence (0) or picture
  606.      header has been parsed (1) */
  607.  
  608.   ret = Get_Hdr();
  609.  
  610.  
  611.   if (Two_Streams)
  612.   {
  613.     ld = &enhan;
  614.     if (Get_Hdr()!=ret && !Quiet_Flag)
  615.       fprintf(stderr,"streams out of sync\n");
  616.     ld = &base;
  617.   }
  618.  
  619.   return ret;
  620. }
  621.  
  622.  
  623. static int Decode_Bitstream()
  624. {
  625.   int ret;
  626.   int Bitstream_Framenum;
  627.  
  628.   Bitstream_Framenum = 0;
  629.  
  630.   for(;;)
  631.   {
  632.  
  633. #ifdef VERIFY
  634.     Clear_Verify_Headers();
  635. #endif /* VERIFY */
  636.  
  637.     ret = Headers();
  638.     
  639.     if(ret==1)
  640.     {
  641.       ret = video_sequence(&Bitstream_Framenum);
  642.     }
  643.     else
  644.       return(ret);
  645.   }
  646.  
  647. }
  648.  
  649.  
  650. static void Deinitialize_Sequence()
  651. {
  652.   int i;
  653.  
  654.   /* clear flags */
  655.   base.MPEG2_Flag=0;
  656.  
  657.   for(i=0;i<3;i++)
  658.   {
  659.     free(backward_reference_frame[i]);
  660.     free(forward_reference_frame[i]);
  661.     free(auxframe[i]);
  662.  
  663.     if (base.scalable_mode==SC_SPAT)
  664.     {
  665.      free(llframe0[i]);
  666.      free(llframe1[i]);
  667.     }
  668.   }
  669.  
  670.   if (base.scalable_mode==SC_SPAT)
  671.     free(lltmp);
  672.  
  673. #ifdef DISPLAY
  674.   if (Output_Type>=T_AMIGADIRECT) 
  675.     Terminate_Display_Process();
  676. #endif
  677. }
  678.  
  679.  
  680. static int video_sequence(int *Bitstream_Framenumber)
  681. {
  682.   extern unsigned long audioBufferSize;
  683.   static unsigned short *rmb = (unsigned short *)0xdff016;
  684.   int Bitstream_Framenum;
  685.   int Sequence_Framenum;
  686.   int Return_Value;
  687.  
  688.   Bitstream_Framenum = *Bitstream_Framenumber;
  689.   Sequence_Framenum=0;
  690.  
  691.   Initialize_Sequence();
  692.  
  693.   /* decode picture whose header has already been parsed in 
  694.      Decode_Bitstream() */
  695.  
  696.  
  697.   Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
  698.   Decode_Audio();
  699.  
  700.   /* update picture numbers */
  701.   if (!Second_Field)
  702.   {
  703.     Bitstream_Framenum++;
  704.     Sequence_Framenum++;
  705.   }
  706.  
  707.   /* loop through the rest of the pictures in the sequence */
  708.   while ((Return_Value = Headers()))
  709.   {
  710.     Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
  711.     Decode_Audio();
  712.     while(audioBufferSize>5120) Decode_Audio();
  713.  
  714.     if (!Second_Field)
  715.     {
  716.       Bitstream_Framenum++;
  717.       Sequence_Framenum++;
  718.     }
  719.  
  720.     /* Seek with right mouse button */
  721.     if ((*rmb & 0x400) == 0) {
  722.       unsigned char *mousex = (unsigned char *)0xdff00b;
  723.       unsigned char mpos = *mousex;
  724.       unsigned char newmpos;
  725.       int oldEnaSnd = Enable_Sound;
  726.       int oldTCFlg = TimeCode_Flag;
  727.       int delta;
  728.  
  729.       Enable_Sound = 0;
  730.       TimeCode_Flag = 1;
  731.  
  732.       while ((*rmb & 0x400) == 0) {
  733.         newmpos = *mousex;
  734.         delta = (int)(newmpos - mpos);
  735.         if (delta >= 128)
  736.           delta -= 256;
  737.         else if (delta <= -128)
  738.           delta += 256;
  739.         mpos = newmpos;
  740.         if (delta == 0)
  741.           Return_Value = Headers();
  742.         else
  743.           Return_Value = Get_Hdr_Seek(delta);
  744.         if (Return_Value == 0)
  745.           break;
  746.         Decode_Picture(Bitstream_Framenum++, Sequence_Framenum++);
  747.       }
  748.       Enable_Sound = oldEnaSnd;
  749.       TimeCode_Flag = oldTCFlg;
  750.       if (Return_Value == 0)
  751.         break;
  752.     }
  753.   }
  754.  
  755.   /* put last frame */
  756.   if (Sequence_Framenum!=0)
  757.   {
  758.     Output_Last_Frame_of_Sequence(Bitstream_Framenum);
  759.     while(Decode_Audio());
  760.   }
  761.  
  762.   Deinitialize_Sequence();
  763.  
  764. #ifdef VERIFY
  765.     Clear_Verify_Headers();
  766. #endif /* VERIFY */
  767.  
  768.   *Bitstream_Framenumber = Bitstream_Framenum;
  769.   return(Return_Value);
  770. }
  771.  
  772.  
  773. static void Clear_Options()
  774. {
  775.   Verbose_Flag = 0;
  776.   Output_Type = 0;
  777.   Output_Picture_Filename = " ";
  778.   hiQdither  = 0;
  779.   Output_Type = 0;
  780.   Frame_Store_Flag = 0;
  781.   Spatial_Flag = 0;
  782.   Lower_Layer_Picture_Filename = " ";
  783.   Reference_IDCT_Flag = 0;
  784.   Trace_Flag = 0;
  785.   Quiet_Flag = 0;
  786.   Ersatz_Flag = 0;
  787.   Substitute_Picture_Filename  = " ";
  788.   Two_Streams = 0;
  789.   Enhancement_Layer_Bitstream_Filename = " ";
  790.   Big_Picture_Flag = 0;
  791.   Main_Bitstream_Flag = 0;
  792.   Main_Bitstream_Filename = " ";
  793.   Verify_Flag = 0;
  794.   Stats_Flag  = 0;
  795.   User_Data_Flag = 0; 
  796.   Mode_ID = INVALID_ID;
  797.   Enable_Sound = 0L;
  798.   Double_Pixels = 1L;
  799.   Fps_Flag = 0;
  800.   TimeCode_Flag = 0;
  801.   AllFrames = 0;
  802.   User_Defined_FPS = 0.0;
  803. }
  804.  
  805.  
  806. #ifdef DEBUG
  807. static void Print_Options()
  808. {
  809.   
  810.   printf("Verbose_Flag                         = %d\n", Verbose_Flag);
  811.   printf("Output_Type                          = %d\n", Output_Type);
  812.   printf("Output_Picture_Filename              = %s\n", Output_Picture_Filename);
  813.   printf("hiQdither                            = %d\n", hiQdither);
  814.   printf("Output_Type                          = %d\n", Output_Type);
  815.   printf("Frame_Store_Flag                     = %d\n", Frame_Store_Flag);
  816.   printf("Spatial_Flag                         = %d\n", Spatial_Flag);
  817.   printf("Lower_Layer_Picture_Filename         = %s\n", Lower_Layer_Picture_Filename);
  818.   printf("Reference_IDCT_Flag                  = %d\n", Reference_IDCT_Flag);
  819.   printf("Trace_Flag                           = %d\n", Trace_Flag);
  820.   printf("Quiet_Flag                           = %d\n", Quiet_Flag);
  821.   printf("Ersatz_Flag                          = %d\n", Ersatz_Flag);
  822.   printf("Substitute_Picture_Filename          = %s\n", Substitute_Picture_Filename);
  823.   printf("Two_Streams                          = %d\n", Two_Streams);
  824.   printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename);
  825.   printf("Big_Picture_Flag                     = %d\n", Big_Picture_Flag);
  826.   printf("Main_Bitstream_Flag                  = %d\n", Main_Bitstream_Flag);
  827.   printf("Main_Bitstream_Filename              = %s\n", Main_Bitstream_Filename);
  828.   printf("Verify_Flag                          = %d\n", Verify_Flag);
  829.   printf("Stats_Flag                           = %d\n", Stats_Flag);
  830.   printf("User_Data_Flag                       = %d\n", User_Data_Flag);
  831.   printf("Mode_ID                              = %08lx\n", Mode_ID);
  832.   printf("FPS_Counter_Flag                     = %d\n", Fps_Flag);
  833.   printf("TimeCode_Flag                        = %d\n", TimeCode_Flag);
  834.   printf("AllFrames                            = %d\n", AllFrames);
  835.   printf("User_Defined_FPS                     = %f\n", User_Defined_FPS);
  836. }
  837. #endif
  838.